<?php

namespace app\index\controller;

use think\Controller;
use think\Loader;

class Test extends Controller
{
    public function testAlipay()
    {
        Loader::import('Alipay.aop.AopClient');
        $aop = new \AopClient();

        $config = config('alipay');
        $aop->gatewayUrl = $config['gatewayUrl'];
        $aop->appId = $config['appId'];
        $aop->rsaPrivateKey = $config['rsaPrivateKey'];
        $aop->format = $config['format'];
        $aop->postCharset = $config['postCharset'];
        $aop->signType = $config['signType'];
        $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];

        Loader::import('Alipay.aop.request.AlipayTradeAppPayRequest');

        $request = new \AlipayTradeAppPayRequest();

        $bizcontentArr= array(

            'subject'=>'龙门小筑',
            'out_trade_no'=>2020021400001,
            'total_amount'=>0.01,
            'product_code'=>'QUICK_MSECURITY_PAY'
        );

        $request->setNotifyUrl('http://api.lycbb.com/Home/Public/noticeAlipayAsetmeal');
        $bizcontent = json_encode($bizcontentArr);
        $request->setBizContent($bizcontent);
        $response = $aop->sdkExecute($request);

        dump(['code'=>'200','msg'=>'请求成功','data'=>['payUrl'=>$response]]);

    }

    public function testWechatPay(){

        //微信支付
        Loader::import('WxPay.WxPay',EXTEND_PATH,'.Api.php');
        $config = new \WxPayConfig();
        $WxPay  = new \WxPayApi();
        $WxData = new \WxPayUnifiedOrder();

        $dataArr = array(

            'out_trade_no'     => 20200212000999991,            //订单号
            'body'             => '龙们小筑',                         //商品名
            'total_fee'        => 100,        //支付总金额
            'trade_type'       => 'APP',                     //支付途径
            'nonce_str'        => $WxPay->getNonceStr(),    //32位随进数
            'notify_url'       => 'http://api.lycbb.com/Home/Public/noticePayWx',// 异步通通知地址
            'spbill_create_ip' => $_SERVER['REMOTE_ADDR']
        );

        $WxData->SetOut_trade_no($dataArr['out_trade_no']);
        $WxData->SetBody($dataArr['body']);
        $WxData->SetTotal_fee($dataArr['total_fee']);
        $WxData->SetTrade_type($dataArr['trade_type']);
        $WxData->SetNonce_str($dataArr['nonce_str']);
        $WxData->SetNotify_url($dataArr['notify_url']);
        $WxData->SetSpbill_create_ip($dataArr['spbill_create_ip']);
        $WxData->SetSign($config);
        var_dump($WxData);
        $return = $WxPay->unifiedOrder($config,$WxData);



        if (array_key_exists('result_code',$return) && array_key_exists('return_code',$return) && $return['result_code'] == 'SUCCESS' && $return['return_code'] == 'SUCCESS'){
            $signArr = array(
                'appid'     =>'wx3e1304e265cad988',
                'partnerid' =>'1508875671',
                'timestamp' =>time(),
                'package'   =>'Sign=WXPay',
                'noncestr'  =>$return['nonce_str'],
                'prepayid'  =>$return['prepay_id']
            );
            $setSignClass = new \WxPayResults();

            $setSignClass->SetData('sign',$return['sign']);
            $setSignClass->SetData('appid',$signArr['appid']);
            $setSignClass->SetData('partnerid',$signArr['partnerid']);
            $setSignClass->SetData('timestamp',$signArr['timestamp']);
            $setSignClass->SetData('package',$signArr['package']);
            $setSignClass->SetData('noncestr',$signArr['noncestr']);
            $setSignClass->SetData('prepayid',$signArr['prepayid']);
            $signToApp = $setSignClass->MakeSign($config);


            echo "<pre>";
            print_r(array('code'=>'200','msg'=>'请求成功','data'=>array('prepay_id'=>$return['prepay_id'],'sign'=>$signToApp,'timestamp'=>$signArr['timestamp'],'noncestr'=>$signArr['noncestr'])));

            dump(array('code'=>'200','msg'=>'请求成功','data'=>array('prepay_id'=>$return['prepay_id'],'sign'=>$signToApp,'timestamp'=>$signArr['timestamp'],'noncestr'=>$signArr['noncestr'])));
        }else{
            echo json_encode(array('code'=>'401','msg'=>'提交失败'));
        }
    }

    function request_by_curl($remote_server, $post_string) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }

    public function testError(){

    }

    public function test(){

        echo dirname(__FILE__).'../../extend/isWechatPayCret';

    }



}
